package com.dd.cloud.user.service.aicms.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.interceptor.UserInterceptor;
import com.dd.cloud.common.utils.StringOperUtil;
import com.dd.cloud.common.vo.LoginInfo;
import com.dd.cloud.user.config.ZerkProperties;
import com.dd.cloud.user.entity.aicms.AicmsDepartment;
import com.dd.cloud.user.mapper.aicms.AicmsDepartmentMapper;
import com.dd.cloud.user.res.aicms.AllDdpRes;
import com.dd.cloud.user.service.aicms.IAicmsDepartmentService;
import com.dd.cloud.user.service.role.IAdminPowerService;
import com.dd.cloud.user.vo.DepartmentVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author czg
 * @since 2022-04-27
 */
@Service
@Slf4j
public class AicmsDepartmentServiceImpl extends ServiceImpl<AicmsDepartmentMapper, AicmsDepartment> implements IAicmsDepartmentService {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private ZerkProperties zerkProperties;
    @Autowired
    private IAdminPowerService adminPowerService;

    @Override
    public String keyInfo() {
        try {
            String str = restTemplate.getForObject("https://www.zrkmy.com/pyapi/getsoftkeyinfo?companyname=泽科", String.class);
            str = StringOperUtil.decodeUnicode(str);
            return str;
        } catch (Exception e) {
            return "接口unioncode解码异常, 请直接访问https://www.zrkmy.com/pyapi/getsoftkeyinfo?companyname=泽科";
        }
    }

    /**
     * 获取所有科室
     *
     * @return
     */
    @Override
    public List<AicmsDepartment> queryDepartMents(String sex, String age) {
        List<AicmsDepartment> list = list();
        UserInterceptor.getLoginUserInfo().get();
        if (list == null || list.isEmpty()) {
            //本地无数据,请求接口
            try {
                String str = restTemplate.getForObject(zerkProperties.getKeShiUrl(), String.class);
                DepartmentVo vo = JSON.parseObject(str, DepartmentVo.class);
                log.info("获取科室：" + vo);
                if (vo != null && vo.getnErrorCode() == 0) {
                    list = vo.getKeshi();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (list != null) {
            if (sex != null) {
                if ("男".equals(sex)) {
                    list = list.stream().filter(t -> !t.getSName().equals("妇科")).collect(Collectors.toList());
                } else if ("女".equals(sex)) {
                    list = list.stream().filter(t -> !t.getSName().equals("男科")).collect(Collectors.toList());
                }
            }
            if (age != null) {
                int nl;
                if (age.contains(".")) {
                    nl = Integer.parseInt(age.split("\\.")[0]);
                } else {
                    nl = Integer.parseInt(age);
                }
                if (nl > 17) {
                    list = list.stream().filter(t -> !t.getSName().equals("儿科")).collect(Collectors.toList());
                }
                if (nl < 60) {
                    list = list.stream().filter(t -> !t.getSName().equals("老年疾病")).collect(Collectors.toList());
                }
            }
        }
        log.info("查询所有科室:{}", list);
        return list;
    }

    /**
     * 获取能配置的科室
     *
     * @return
     */
    @Override
    public List<AicmsDepartment> queryConfigDepartMents() {
        List<AicmsDepartment> list = list();
        if (list == null || list.isEmpty()) {
            //本地无数据,请求接口
            try {
                String str = restTemplate.getForObject(zerkProperties.getKeShiUrl(), String.class);
                DepartmentVo vo = JSON.parseObject(str, DepartmentVo.class);
                log.info("获取科室：" + vo);
                if (vo != null && vo.getnErrorCode() == 0) {
                    list = vo.getKeshi();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo().get();
        if (loginInfo.getType() == LoginInfo.UserType.ADMIN) {
            List<String> names = adminPowerService.getDataRoleByUser();
//        List<String> ids = adminSetDepService.getAdminSetDep();
            list = list.stream().filter(t -> names.contains(t.getSName())).collect(Collectors.toList());
        }
        log.info("查询所有科室:{}", list);
        return list;
    }

    /**
     * 查询所有的科室疾病
     *
     * @return
     */
    @Override
    public List<AllDdpRes> getAllDepDis() {
        String cacheCode = redisTemplate.opsForValue().get("all_dep_dis");
        List<AllDdpRes> list = new ArrayList<>();
        if (StringUtils.isBlank(cacheCode)) {
            list = getBaseMapper().getAllDisease();
            redisTemplate.opsForValue().set("all_dep_dis", JSONArray.toJSONString(list));
        } else {
            list = JSONObject.parseArray(cacheCode, AllDdpRes.class);
        }
        return list;
    }

}
